<?php
/**
 * User: hjd-yao
 * Date: 2019/6/6
 * Time: 15:12
 */

namespace Shop\Http\Controllers\Wap;


use App\Helpers\SelfResponse;
use App\Models\Shop\ShopCategoryModel;
use App\Models\Shop\ShopGoodsModel;
use App\Models\Shop\ShopGoodsSpecValueModel;
use Frontend\Models\UserBaseModel;
use Illuminate\Http\Request;
use Shop\Http\Controllers\BaseController;
use Illuminate\Support\Facades\DB;
class GoodsController extends BaseController
{

    public function classify(Request $request)
    {
        if($request->has('cate')){
            $sub_category = ShopCategoryModel::where('status',1)->whereRaw("FIND_IN_SET(?,`parent_ids`)",[$request->input('cate')])->get();
        }else{
            $sub_category = ShopCategoryModel::where('status',1)->where('is_recommend',1)->get();
        }
        $category = ShopCategoryModel::where('status',1)->where('parent_id',0)->get();
        return $this->wapRender('goods.classify',[
            'page_title'=>'分类',
            'category'=>$category,
            'cate_id' =>$request->input('cate',0),
            'sub_category'=>$sub_category
        ]);
    }

    public function search(Request $request)
    {
        $keyword = $request->input('keyword');

        $goodsModel = ShopGoodsModel::where('status',1);
        if(!empty($keyword)){
            $goodsModel = $goodsModel->where(DB::raw('concat(title,goods_name,description,keywords)'),'like','%'.$keyword.'%');
        }
        $pageNum = $request->input('page_num',1);
        $list = $goodsModel->orderBy('sort','desc')->forPage($pageNum,20)->get();
        $category = ShopCategoryModel::where('status',1)->where('parent_id',0)->get();
        return $this->wapRender('goods.search',[
            'page_title'=>'搜索结果',
            'list'=>$list,
            'category'=>$category,
            'keyword'=>$keyword
        ]);

    }

    public function goodsList(Request $request,$zone = 1)
    {
        $cate_id = $request->input('cate',0);
        $goodsModel = ShopGoodsModel::where('goods_zone',$zone)->where('status',1);
        if($cate_id>0){
            $goodsModel = $goodsModel->whereRaw("FIND_IN_SET(?,`category_ids`)",[$cate_id]);
        }
        $pageNum = $request->input('page_num',1);
        $list = $goodsModel->orderBy('sort','desc')->forPage($pageNum,20)->get();
        $category = ShopCategoryModel::where('status',1)->where('parent_id',0)->get();
        return $this->wapRender('goods.list',[
            'page_title'=>config('shop.zone')[$zone],
            'zone'=>$zone,
            'list'=>$list,
            'cate_id'=>$cate_id,
            'category'=>$category
        ]);
    }

    public function goodsDetail(Request $request)
    {
        $goods_id = $request->input('goods_id');
        $goods = ShopGoodsModel::find($goods_id);
        return $this->wapRender('goods.detail',[
            'page_title'=>'商品详情',
            'goods'=>$goods,
        ]);
    }

    public function GetGoodsSpec(Request $request){
      $goods_id =  $request->input('goods_id') ;
      $goods_spec =  $request->input('goods_spec') ;
      $goods_spec =urldecode($goods_spec);
      $spec_info = ShopGoodsSpecValueModel::where('goods_id',$goods_id)->where('goods_spec_items',$goods_spec)->select(['price','vip_price','market_price','stock','fact_sales_volume','show_sales_volume'])->first();
      if($spec_info){
           $data = [
               'status'=>1,
               'data'=>$spec_info
           ];
      }else{
          $data =[
              'status'=>0,
              'data' =>[]
          ];
      }
      return response()->json($data);
    }
}